package service

import (
	"errors"
	"time"
	"vrcm/src/vrcm/app/admin/models/subject"

	"gorm.io/gorm"
	"vrcm/src/vrcm/common/service"

	"vrcm/src/vrcm/app/admin/service/dto/subject"
	"vrcm/src/vrcm/common/actions"
	cDto "vrcm/src/vrcm/common/dto"
)

type Paper struct {
	service.Service
}

// GetPage 获取Paper列表
func (e *Paper) GetPage(c *dto.PaperGetPageReq, p *actions.DataPermission, list *[]subject.Paper, count *int64) error {
	var err error
	var data subject.Paper

	err = e.Orm.Model(&data).
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
		).
		Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("PaperService GetPage error:%s \r\n", err)
		return err
	}
	return nil
}

// Get 获取Paper对象
func (e *Paper) Get(d *dto.PaperGetReq, p *actions.DataPermission, model *subject.Paper) error {
	var data subject.Paper

	err := e.Orm.Model(&data).
		Scopes(
			actions.Permission(data.TableName(), p),
		).
		First(model, d.GetId()).Error
	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		err = errors.New("查看对象不存在")
		e.Log.Errorf("Service GetPaper error:%s \r\n", err)
		return err
	}
	if err != nil {
		e.Log.Errorf("db error:%s", err)
		return err
	}
	return nil
}

// Insert 创建Paper对象
func (e *Paper) Insert(c *dto.PaperInsertReq) error {
	var err error
	var data subject.Paper

	//新增题目判断重复
	e.Orm.Model(&subject.Paper{}).Where(&subject.Paper{PaperName: c.PaperName, IsDelete: 2}).First(&data)
	if data.Model.Id > 0 {
		err = errors.New("试卷已经存在,请重新创建")
		e.Log.Errorf("SubjectService Insert error:%s \r\n", err)
		return err
	}
	c.Generate(&data)
	err = e.Orm.Create(&data).Error
	if err != nil {
		e.Log.Errorf("PaperService Insert error:%s \r\n", err)
		return err
	}
	return nil
}

// Update 修改Paper对象
func (e *Paper) Update(c *dto.PaperUpdateReq, p *actions.DataPermission) error {
	var err error
	var data = subject.Paper{}

	//修改后的题目不能重复
	e.Orm.Model(&subject.Paper{}).Where(&subject.Paper{PaperName: c.PaperName, IsDelete: 2}).Not("id = ?", c.Id).First(&data)
	if data.Model.Id > 0 {
		err = errors.New("题目已经存在")
		e.Log.Errorf("SubjectService update error:%s \r\n", err)
		return err
	}
	e.Orm.First(&data, c.GetId())
	c.Generate(&data)

	db := e.Orm.Save(&data)
	if err = db.Error; err != nil {
		e.Log.Errorf("PaperService Save error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权更新该数据")
	}
	return nil
}

// Remove 删除Paper
func (e *Paper) Remove(d *dto.PaperDeleteReq) error {

	value := make(map[string]interface{})
	value["is_delete"] = 1
	value["update_time"] = time.Now().Unix()
	tx := e.Orm.Begin()
	err := tx.Model(&subject.Paper{}).Where("id in (?)", d.GetId()).Updates(value).Error
	if err != nil {
		tx.Rollback()
		e.Log.Errorf("Service RemovePaper error:%s \r\n", err)
		return err
	}

	err = e.Orm.Model(&subject.Subject{}).Where("paper_id = ?", d.GetId()).Updates(value).Error
	if err != nil {
		tx.Rollback()
		e.Log.Errorf("Service RemovePaper error:%s \r\n", err)
		return err
	}
	tx.Commit()
	return nil
}
